type File native  // rx.File

type FileState open record {
    Name String,
    Size Int,
    Mode FileMode,
    IsDir Bool,
    ModTime Time
}

type FileMode native

type FileOpenMode union {
    const
        FileReadOnly,
        FileReadWrite,
        FileReadWriteCreate,
        FileOverwrite,
        FileAppend
}


public function
    WalkDirectory { dir Path } Observable[Pair[Path,FileState]]
    native('__SysWalkDir')

public function
    ListDirectory { dir Path } Observable[Pair[Path,FileState]]
    native('__SysListDir')


public function
    OpenFilePersist { file Path, mode FileOpenMode } Observable[File]
    native('__SysOpenFilePersist')

public function
    OpenFileConsume[T] { file Path, mode FileOpenMode, k Lambda[File,Observable[T]] } Observable[T] {
        \ await f = OpenFilePersist(file, mode),
        \ await result = (k(f) on-cancel f.Close),
        \ await f.Close,
        Return(result)
    }

public method
    File.Close Observable[None]
    native('__SysFileClose')


public method
    File.State Observable[FileState]
    native('__SysFileGetState')

public method
    File.SeekStart Lambda[Int,Observable[Int]]
    native('__SysFileSeekStart')

public method
    File.SeekDelta Lambda[Int,Observable[Int]]
    native('__SysFileSeekDelta')

public method
    File.SeekEnd Lambda[Int,Observable[Int]]
    native('__SysFileSeekEnd')

public method
    File.Read Lambda[Int,Observable[Bytes]]
    native('__SysFileRead')

public method
    File.ReadAll Observable[Bytes]
    native('__SysFileReadAll')

public method
    File.ReadChar Observable[Char]
    native('__SysFileReadChar')

public method
    File.ReadLine Observable[String]
    native('__SysFileReadLine')

public method
    File.ReadLines Observable[String]
    native('__SysFileReadLines')

public method
    File.Write Lambda[Bytes,Observable[None]]
    native('__SysFileWrite')

public method
    File.WriteString Lambda[String,Observable[None]]
    native('__SysFileWriteString')

public method
    File.WriteLine Lambda[String,Observable[None]]
    native('__SysFileWriteLine')


